<?php

/**
 * Description of DashboardController
 *
 * @author Nylaye
 */
class Storer_AdminController extends Model3_Controller
{

    public function init()
    {
        if (!Model3_Auth::isAuth())
        {
            $this->redirect('Index/index');
        }
    }

    public function usersAction()
    {
        $request = $this->getRequest();
        if ($request->isPost() === true)
        {
            $params = $request->getPost();
            $this->view->successAssociate = $this->associateExchangeCenter($params);
        }
        $em = $this->getEntityManager('DefaultDb');
        $usersRepository = $em->getRepository('DefaultDb_Entities_User');
        $storer = $usersRepository->getActiveUserStorer();
        $this->view->whereAmI = array();
        $this->view->setTemplate('Responsive-3.0');
        $this->view->title = 'Administración de almacenistas';
        $this->view->description = 'Configuración básica de los almacenistas';
        $this->view->storer = $storer;
    }

    /**
     * Asociamos un centro de intercambio a un almacenista
     * @param array $params
     * @return boolean true si se asocio el centro de intercambio con el almacenista
     */
    private function associateExchangeCenter(array $params)
    {
        $success = false; // :(
        if (isset($params['storerId']) === true && isset($params['exchangeCenterId']) === true)
        {
            $em = $this->getEntityManager('DefaultDb');
            $usersRepository = $em->getRepository('DefaultDb_Entities_User');
            $pointRepository = $em->getRepository('DefaultDb_Entities_Point');
            $storerId = $params['storerId'];
            $exchangeCenterId = $params['exchangeCenterId'];
            $storer = $usersRepository->find($storerId);
            $ci = $pointRepository->find($exchangeCenterId);
            if ($storer instanceof DefaultDb_Entities_User && $ci instanceof DefaultDb_Entities_Point)
            {
                $storer->setPoint($ci);
                $em->persist($storer);
                $em->flush();
                $success = true; // :)
            }
        }
        return $success;
    }

    public function associateExchangeCenterAction()
    {
        $storerId = $this->getRequest()->getParam('storerId');
        if ($storerId !== null)
        {
            $em = $this->getEntityManager('DefaultDb');
            $userRepository = $em->getRepository('DefaultDb_Entities_User');
            $storer = $userRepository->find($storerId);
            if ($storer !== null)
            {
                $pointRepository = $em->getRepository('DefaultDb_Entities_Point');
                $exchangesCenters = $pointRepository->getActiveExchangesCenters();
                $this->view->setTemplate('Responsive-3.0');
                $this->view->storer = $storer;
                $this->view->exchangeCenters = $exchangesCenters;
                $this->view->whereAmI = array('Administración de almacenista' => $this->view->url(array(
                        'controller' => 'Admin', 'action' => 'users')));
                $this->view->title = 'Asignar centro de intercambio';
                $this->view->description = 'Asociar almacenistas a un centro de intercambio vigente';
            }
            else
            {
                $this->redirect('Storer/Admin/users');
            }
        }
        else
        {
            $this->redirect('Storer/Admin/users');
        }
    }

}